package com.zby.business.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zby.business.R;
import com.zby.business.net.MyAsyncTask;
import com.zby.business.util.Util;

import java.util.concurrent.ExecutionException;

/**
 * Created by BY on 2016/1/15.
 */
public class Register extends Activity implements View.OnClickListener {
    private TextView titleTextView;
    private RelativeLayout backLayout;
    private Button registerButton;
    private RadioGroup radioGroup;
    private String sex = "male";
    private InputMethodManager imm;
    private EditText phoneEditText, emailEditText, nameEditText, passwordEditText, userNameEditText, IDCardEditText,addressEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        init();
    }

    private void init() {
        LinearLayout layout = (LinearLayout) findViewById(R.id.registerLayout);
        layout.setOnClickListener(this);
        titleTextView = (TextView) findViewById(R.id.headerTitle);
        titleTextView.setText("注册账号");
        backLayout = (RelativeLayout) findViewById(R.id.backLayout);
        backLayout.setOnClickListener(this);
        registerButton = (Button) findViewById(R.id.registerButton);
        registerButton.setOnClickListener(this);
        passwordEditText = (EditText) findViewById(R.id.passwordEdittext);
        userNameEditText = (EditText) findViewById(R.id.userNameEdittext);
        nameEditText = (EditText) findViewById(R.id.nameEdittext);
        addressEditText = (EditText) findViewById(R.id.addressEdittext);
        imm = (InputMethodManager) nameEditText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        emailEditText = (EditText) findViewById(R.id.emailEdittext);
        IDCardEditText = (EditText) findViewById(R.id.IDCardEdittext);
        phoneEditText = (EditText) findViewById(R.id.phoneEdittext);
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.maleRadio) {
                    sex = "male";
                } else {
                    sex = "female";
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.backLayout:
                finish();
                break;
            case R.id.registerLayout:
                if (imm.isActive()) {
                    imm.hideSoftInputFromWindow(nameEditText.getWindowToken(), 0);
                }
                break;
            case R.id.registerButton:
                String phone = phoneEditText.getText().toString().trim();
                String name = nameEditText.getText().toString().trim();
                String email = emailEditText.getText().toString().trim();
                String IDCard = IDCardEditText.getText().toString().trim();
                String password = passwordEditText.getText().toString().trim();
                String userName = userNameEditText.getText().toString().trim();
                String address = addressEditText.getText().toString().trim();
                if (phone.equals("") || name.equals("") || email.equals("") || IDCard.equals("") || password.equals("") || userName.equals("")||address.equals("")) {
                    Util.toast(getApplicationContext(), "请填写完整信息");
                } else {
                    MyAsyncTask asyncTask = new MyAsyncTask();
                    try {
                        String s = asyncTask.execute("phoneRegister?name=" + name + "&phone=" + phone + "&email=" + email + "&IDCard=" + phone + "&password=" + password + "&userName=" + userName + "&sex=" + sex+"&address="+address).get();
                        System.out.println(s);
                        if (s.equals("exist")) {
                            Util.toast(getApplicationContext(), "用户已存在，换一个");
                        } else {
                            finish();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                break;
        }
    }
}
